package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/2/27.
 */
public class LC06 {

    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }

        int len = s.length();
        int numCols = len / (2 * (numRows - 1)) + 1;
        StringBuilder resBuilder = new StringBuilder();
        //start to add first row
        int index = 0;
        while (index < len) {
            resBuilder.append(s.charAt(index));
            index += 2 * (numRows - 1);
        }
        // start to add middle row
        for (int row = 1; row < numRows - 1; ++row) {
            index = row;
            boolean switchFlag = true;
            while (index < len) {
                resBuilder.append(s.charAt(index));
                if (switchFlag) {
                    index += 2 * (numRows - row - 1);
                    switchFlag = false;
                } else {
                    index += 2 * (row);
                    switchFlag = true;
                }
            }
        }

        // start to last row
        index = numRows - 1;
        while (index < len) {
            resBuilder.append(s.charAt(index));
            index += 2 * (numRows - 1);
        }

        return resBuilder.toString();
    }

    /**
     * Input: s = "PAYPALISHIRING", numRows = 3
     * Output: "PAHNAPLSIIGYIR"
     */
    public static void main(String[] args) {
        LC06 an = new LC06();
        String str = "abcdefghijklmno";
        System.out.println(an.convert("PAYPALISHIRING", 3));
    }
}
